#include "EWAUIRuleWidget.hpp"

namespace Eyer
{
    int EWAUIRuleWidget::OnDraw()
    {
        RemoveAllUIGraph();

        EyerSmartPtr<EWAUIGraphRect> rect(new EWAUIGraphRect());
        rect->xy.SetX(0);
        rect->xy.SetY(0);
        rect->wh = wh;
        rect->background = EyerColor::GRAY_34;
        AddUIGraph(rect);

        EyerSmartPtr<EWAUIGraphLine> bottomLine(new EWAUIGraphLine());
        bottomLine->xyStart.x(0.0);
        bottomLine->xyStart.y(wh.h() - 1);
        bottomLine->xyEnd.x(wh.w());
        bottomLine->xyEnd.y(wh.h() - 1);
        bottomLine->linecolor = EyerColor::BLACK;
        AddUIGraph(bottomLine);


        int minStep = 5; // 最小步长 5 个像素
        int stepTime = 500; // 一个格子代表多少毫秒

        //  获取总长度
        int uiWidth = wh.w();
        for(int i = 0; i < uiWidth / minStep; i++){
            EyerSmartPtr<EWAUIGraphLine> line(new EWAUIGraphLine());

            line->xyStart.x(i * minStep);
            line->xyStart.y(0);

            line->xyEnd.x(i * minStep);
            if(i % 2 != 0){
                line->xyEnd.y(wh.h() * 0.2);
            }
            else {
                line->xyEnd.y(wh.h() * 0.3);
            }
            line->linecolor = EyerColor::GRAY_85;

            if(i % 20 == 0){
                line->xyEnd.y(wh.h() * 0.4);
                line->linecolor = EyerColor::GRAY_119;
            }
            if(i % 40 == 0){
                line->xyEnd.y(wh.h() * 0.6);
                line->linecolor = EyerColor::GRAY_170;

                EyerString secTime = EyerString::FormatSec(i * stepTime / 1000);

                EyerSmartPtr<EWAUIGraphText> text(new EWAUIGraphText());
                text->xy.x(i * minStep + 5);
                text->xy.y(wh.h() * 0.4);
                text->textcolor = EyerColor::GRAY_238;
                text->str = secTime + ":00";

                AddUIGraph(text);
            }

            AddUIGraph(line);
        }

        return 0;
    }

    EWAUIRuleWidget::EWAUIRuleWidget(EWAUILayoutParams width, EWAUILayoutParams height)
        : EWAUIWidget(width, height)
    {
        widgetName = "EWAUIRuleWidget";
        SetMinWH(0, ruleHeight);
    }

    EWAUIRuleWidget::~EWAUIRuleWidget()
    {

    }
}